# -*- coding: UTF-8 -*-
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QPalette, QBrush, QPixmap, QDesktopServices
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, \
    QLineEdit, QMessageBox
from PyQt5 import QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication
import pdfkit
import time


class baogaopage(QWidget):
    def __init__(self):
        super(baogaopage, self).__init__()
        self.resize(1240, 732)
        self.iniUI()
        palette = QPalette()
        pix = QPixmap("background.jpg")
        pix = pix.scaled(1920, 1080)
        palette.setBrush(QPalette.Background, QBrush(pix))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

    def iniUI(self):
        l1 = QtWidgets.QLabel(self)
        l1.setText("安全检测报告")
        font = QtGui.QFont()
        font.setFamily('微软雅黑')
        font.setBold(True)
        l1.setFont(font)
        l1.setStyleSheet("QWidget{font:40pt}")
        # l1.move(120,120)
        l1.resize(1240, 200)
        l1.setAlignment(Qt.AlignCenter)

        self.buttn1 = QPushButton('生成报告')
        self.buttn1.setObjectName('choice')
        self.buttn1.setFixedSize(200, 50)
        self.buttn2 = QPushButton('返回主页')
        self.buttn2.setObjectName('choice')
        self.buttn2.setFixedSize(200, 50)
        self.Hbox1 = QHBoxLayout()
        self.Hbox1.addStretch(1)
        self.Hbox1.addWidget(self.buttn1)
        self.Hbox1.addWidget(self.buttn2)
        self.Hbox1.addStretch(1)

        self.edt1 = QLineEdit()
        self.edt1.setText('')
        self.edt2 = QLineEdit()
        self.edt2.setText('')
        self.edt3 = QLineEdit()
        self.edt3.setText('')
        self.edt4 = QLineEdit()
        self.edt4.setText('')
        self.edt5 = QLineEdit()
        self.edt5.setText('')
        self.edt6 = QLineEdit()
        self.edt6.setText('')
        self.edt1.setFixedSize(500, 40)
        self.edt1.setObjectName("editline")
        self.edt1.setEnabled(False)
        self.edt2.setFixedSize(500, 40)
        self.edt2.setObjectName("editline")
        self.edt2.setEnabled(False)
        self.edt3.setFixedSize(500, 40)
        self.edt3.setObjectName("editline")
        self.edt3.setEnabled(False)
        self.edt4.setFixedSize(500, 40)
        self.edt4.setObjectName("editline")
        self.edt4.setEnabled(False)
        self.edt5.setFixedSize(500, 40)
        self.edt5.setEnabled(False)
        self.edt5.setObjectName("editline")
        self.edt6.setFixedSize(500, 40)
        self.edt6.setEnabled(False)
        self.edt6.setObjectName("editline")
        self.titel1 = QLabel('检测数据集名称：')
        self.titel1.setObjectName('textsize')
        self.titel2 = QLabel('对比数据集名称：')
        self.titel2.setObjectName('textsize')
        self.titel3 = QLabel('数据集详细信息：')
        self.titel3.setObjectName('textsize')
        self.titel4 = QLabel('检   测    模   式：')
        self.titel4.setObjectName('textsize')
        self.titel5 = QLabel('检   测    时   间：')
        self.titel5.setObjectName('textsize')
        self.titel6 = QLabel('检   测    结   果：')
        self.titel6.setObjectName('textsize')
        self.Hbox2 = QHBoxLayout()
        self.Hbox3 = QHBoxLayout()
        self.Hbox4 = QHBoxLayout()
        self.Hbox5 = QHBoxLayout()
        self.Hbox6 = QHBoxLayout()
        self.Hbox7 = QHBoxLayout()
        self.Hbox2.addStretch(1)
        self.Hbox2.addWidget(self.titel1)
        self.Hbox2.addWidget(self.edt1)
        self.Hbox2.addStretch(1)
        self.Hbox3.addStretch(1)
        self.Hbox3.addWidget(self.titel2)
        self.Hbox3.addWidget(self.edt2)
        self.Hbox3.addStretch(1)
        self.Hbox4.addStretch(1)
        self.Hbox4.addWidget(self.titel3)
        self.Hbox4.addWidget(self.edt3)
        self.Hbox4.addStretch(1)
        self.Hbox5.addStretch(1)
        self.Hbox5.addWidget(self.titel4)
        self.Hbox5.addWidget(self.edt4)
        self.Hbox5.addStretch(1)
        self.Hbox6.addStretch(1)
        self.Hbox6.addWidget(self.titel5)
        self.Hbox6.addWidget(self.edt5)
        self.Hbox6.addStretch(1)
        self.Hbox7.addStretch(1)
        self.Hbox7.addWidget(self.titel6)
        self.Hbox7.addWidget(self.edt6)
        self.Hbox7.addStretch(1)
        self.Vbox = QVBoxLayout(self)
        self.Vbox.setSpacing(50)
        self.Vbox.addWidget(l1)
        self.Vbox.addLayout(self.Hbox2)
        self.Vbox.addLayout(self.Hbox3)
        self.Vbox.addLayout(self.Hbox4)
        self.Vbox.addLayout(self.Hbox5)
        self.Vbox.addLayout(self.Hbox6)
        self.Vbox.addLayout(self.Hbox7)
        self.Vbox.addLayout(self.Hbox1)
        self.buttn1.clicked.connect(self.create_pdf)

    def create_pdf(self):
        f = open('report_template.html', 'r', encoding='utf-8')
        d = f.read()
        b = d.replace('检测数据名称：', f'检测数据名称：{self.edt1.text()}')
        d = b.replace('对比数据名称：', f'对比数据名称：{self.edt2.text()}')
        b = d.replace('数据详细信息：', f'数据详细信息：{self.edt3.text()}')
        d = b.replace('检测模式：', f'检测模式：{self.edt4.text()}')
        b = d.replace('检测结果：', f'检测结果：{self.edt6.text()}')
        d = b.replace('检测时间：', f'检测时间：{self.edt5.text()}')
        path = f'report{time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())}.pdf'
        try:
            pdfkit.from_string(d, path)
            QMessageBox.information(self, "提示", "生成完成")
            QDesktopServices.openUrl(QUrl.fromLocalFile(path))
        except:
            QMessageBox.information(self, "提示", "缺少wkhtmltopdf组件，请前往https://wkhtmltopdf.org/downloads.html下载")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(open("./UnFrameStyle.qss").read())
    window = baogaopage()
    window.show()
    sys.exit(app.exec_())
